package com.zhp.web;

import com.alibaba.fastjson.JSON;
import com.zhp.common.R;
import com.zhp.pojo.HUser;
import com.zhp.pojo.HUserinfo;
import com.zhp.service.OrderService;
import com.zhp.service.impl.OrderServiceImpl;
import com.zhp.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @Author Lone Ranger
 */
@WebServlet("/upYyzt")
public class updateYyztServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //type
        // 1       未付款  要取消未付款的单子
        // 2       未付款  想要付款
        // 3       已付款  要取消已付款的单子  这个会有退款操作  ，  取消已付款的单子 是有限制的
        // 4       已付款  要完成已付款的单子   只有完成状态的单子 才可以评论
        String type = req.getParameter("type");
        String oid = req.getParameter("oid");
        String zt = req.getParameter("zt");
        String odate = req.getParameter("odate");

        Date date = DateUtil.strToDate(odate);
        // 从session中取出患者信息
        HttpSession session = req.getSession();
        HUser hUser = (HUser) session.getAttribute("u");
        HUserinfo ui = hUser.getUi();

        OrderService orderService = new OrderServiceImpl();
        //参数1  预约单id
        //参数2  要把这个预约单改为什么状态
        //参数3  这次操作的类型
        //参数4  患者的id
        //参数5  预约日期
        int result = orderService.updateOrderByType(Integer.parseInt(oid),Integer.parseInt(zt),Integer.parseInt(type),ui.getDid(),date);
        PrintWriter writer = resp.getWriter();
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(1);
        }else{
            r.setCode(0);
        }
        writer.write(JSON.toJSONString(r));


    }
}
